package com.example.rjxmgl.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.example.rjxmgl.entity.dos.Course;
import com.example.rjxmgl.entity.dos.Leave;
import com.example.rjxmgl.entity.dos.StudentCourse;
import com.example.rjxmgl.entity.dos.User;
import com.example.rjxmgl.form.CourseAndIsChoose;
import com.example.rjxmgl.form.LoginForm;
import com.example.rjxmgl.mapper.UserMapper;
import com.example.rjxmgl.service.UserService;
import com.example.rjxmgl.utils.ResultMessage;
import com.example.rjxmgl.utils.ResultUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 用户表
 *
 * @author khai 951992121@qq.com
 * @since 1.0.0 2024-12-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Override
    public ResultMessage login(LoginForm login) {

        User user = this.query().eq("email",login.getEmail()).one();
        if (user==null){
            return ResultUtil.error(400,"账号不存在");
        }

        if(!user.getPassword().equals(login.getPassword())){
            return ResultUtil.error(400,"密码错误");
        }

        return ResultUtil.data(user);

    }
    //123

    @Override
    public ResultMessage chooseCourse(Long studentId, Long courseId) {

        StudentCourse one = Db.query(StudentCourse.class).eq("course_id", courseId).eq("student_id", studentId).one();
        if(one!=null){
            return ResultUtil.error(400,"已选课");
        }
        one=new StudentCourse();
        one.setCourseId(courseId);
        one.setStudentId(studentId);
        Db.save(one);
        return ResultUtil.success();

    }
//
    @Override
    public ResultMessage noChooseCourse(Long studentId, Long courseId) {
        StudentCourse one = Db.query(StudentCourse.class).eq("course_id", courseId).eq("student_id", studentId).one();
        if(one==null){
            return ResultUtil.error(400,"未选课");
        }
        Db.removeById(one);
        return ResultUtil.success();
    }
    

    @Override
    public ResultMessage listCourse(Long studentId) {

        List<Course> list = Db.list(Course.class);
        ArrayList<CourseAndIsChoose> targetList = new ArrayList<>(list.size());
        for (Course course : list) {
            CourseAndIsChoose caic = new CourseAndIsChoose();
            caic.setCourse(course);
            StudentCourse one = Db.query(StudentCourse.class).eq("student_id", studentId)
                    .eq("course_id", course.getCourseId()).one();
            if(one==null){
                caic.setIsChoose(false);
            }else{
                caic.setIsChoose(true);
            }
            targetList.add(caic);
        }
        return ResultUtil.data(targetList);
    }

    @Override
    public ResultMessage getTeacherNameById(Long teacherId) {
        String teacherName = Db.query(User.class).eq("id", teacherId).one().getName();
        return ResultUtil.data(teacherName);
    }

    //学生申请请假了
    @Override
    public ResultMessage studentAskForLeave(Leave leave) {

        User student = this.query().eq("id", leave.getStudentId()).one();

        UUID uuid = UUID.randomUUID();
        String randomId = uuid.toString();
        leave.setId(randomId);
        leave.setStatus(0);
        leave.setHeadTeacherId(student.getHeadTeacherId());
        leave.setCounselorId(student.getCounselorId());

        Db.save(leave);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage headTeacherGetList(Long headTeacherId) {
        List<Leave> list = Db.query(Leave.class).eq("head_teacher_id", headTeacherId).list();
        return ResultUtil.data(list);
    }

    @Override
    public ResultMessage headTeacherCheck(Long leaveId, boolean result) {

        Leave le = Db.query(Leave.class).eq("id", leaveId).one();
        if(result){
            le.setStatus(1);
        }else {
            le.setStatus(3);
        }
        Db.updateById(le);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage CounselorGetList(Long counselorId) {
        List<Leave> list = Db.query(Leave.class).eq("counselor_id", counselorId).eq("status",1).list();
        return ResultUtil.data(list);
    }

    @Override
    public ResultMessage counselorCheck(Long leaveId, boolean result) {
        Leave le = Db.query(Leave.class).eq("id", leaveId).one();
        if(result){
            le.setStatus(2);
        }else {
            le.setStatus(3);
        }
        Db.updateById(le);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage studentGetList(Long studentId) {
        List<Leave> list = Db.query(Leave.class).eq("student_id", studentId).list();
        return ResultUtil.data(list);
    }
}